<?php 
//ini_set('display_errors','On');
header("Content-Type: text/html;charset=utf-8");
include("../function/_nosql.php"); //
include("../function/function.php");
include("../_include.user.judge.php");
require_once "lib/WxPay.Api.php";
require_once "WxPay.JsApiPay.php";
ini_set('date.timezone','Asia/Shanghai');
require_once 'log.php';

//引入你的链接数据库的文件

//初始化日志
$logHandler= new CLogFileHandler("logs/".date('Y-m-d').'.log');
$log = Log::Init($logHandler, 15);

//打印输出数组信息
function printf_info($data)
{
    foreach($data as $key=>$value){
        echo "<font color='#00ff55;'>$key</font> : $value <br/>";
    }
}

//①、获取用户openid
$tools = new JsApiPay();
$openId = $tools->GetOpenid();
//②、统一下单

$input = new WxPayUnifiedOrder();
//读取订单信息
$WIDout_trade_no = $_GET['WIDout_trade_no'];
$aid = $_GET['aid'];
$uid = $_SESSION['kboy_userid'];
$time = date("Y");
$order_num = getClassName("kboy_order","order_num","order_no='".$WIDout_trade_no."'");
$moneys = getClassName("kboy_forum_active","active_all_money","id=$aid");
$money = ($moneys*$order_num);
$subject = getClassName("kboy_forum_active","active_title","id=$aid");
$active_tian = getClassName("kboy_forum_active","active_tian","id=".$aid);
$rUnique = $WIDout_trade_no;//订单号
$order_no = substr($rUnique,0,4);
if($order_no == "0500"){
	$money = '500';
	$subject = "金额充值500";
}else if($order_no == "0501"){
	$money = '1000';
	$subject = "金额充值1000";
}else if($order_no == "0502"){
	$money = '3000';
	$subject = "金额充值3000";
}else{
	if(strstr($WIDout_trade_no,'_')){//申请成为cva
	     mysql_query("update  kboy_user  set user_trade_no='".$WIDout_trade_no."' where uid= '".$_SESSION['kboy_userid']."' ");

		 $money = 50;

		 $subject ='申请成为CVA';
	}else{
		//表单提交
		if(getClassName("kboy_user","user_style","uid=$uid") == 1){
			if(getClassName("kboy_jiangli","hdnian","uid=$uid and hd_jieshu=0") == $time and getClassName("kboy_jiangli","hd_yhui","uid=$uid and hd_jieshu=0") == 1){
			$leiji = getClassName("kboy_jiangli","hd_leiji","uid=$uid and hd_jieshu=0");
			$renshu = getClassName("kboy_jiangli","hdrenshu","uid=$uid and hd_jieshu=0");
				if($order_num == 1){
					if($active_tian < 3 and $renshu > 30){
					$money = ($money*0.90);
					$jl_shuom = "带入新会员".$renshu."人，优惠10%";
					}else if($active_tian < 3 and $leiji > 10 and $leiji < 31){
						$money = ($money*0.95);
						$jl_shuom = "累计参加".$leiji."次，优惠5%";
					}else if($active_tian < 3 and $leiji > 30){
						$money = ($money*0.90);
						$jl_shuom = "累计参加".$leiji."次，优惠10%";
					}
				}else if($order_num == 2 or $order_num == 3){
					if($active_tian < 3 and $renshu > 30){
						$benren = ($moneys*0.90);
						$money = ($order_num-1)*$moneys+$benren;
						$jl_shuom = "带入新会员".$renshu."人，和".($order_num-1)."位朋友，优惠10%";
					}else if($active_tian < 3 and $leiji > 10 and $leiji < 31){
						$benren = ($moneys*0.95);
						$money = ($order_num-1)*$moneys+$benren;
						$jl_shuom = "累计参加".$leiji."次，和".($order_num-1)."位朋友，优惠5%";
					}else if($active_tian < 3 and $leiji > 30){
						$benren = ($moneys*0.9);
						$money = ($order_num-1)*$moneys+$benren;
						$jl_shuom = "累计参加".$leiji."次，和".($order_num-1)."位朋友，优惠10%";
					}
				}else if($active_tian < 3 and $order_num-1 < 6 and  $order_num-1 > 2){
					$benren = ($moneys*0.70);
					$money = ($order_num-1)*$moneys+$benren;
						$jl_shuom = "参加本次活动，带新人".($order_num-1)."位，优惠30%";
				}else if($active_tian < 3 and $order_num-1 > 5){
					$benren = ($moneys*0.50);
					$money = ($order_num-1)*$moneys+$benren;
						$jl_shuom = "参加本次活动，带新人".($order_num-1)."位，优惠50%";
				}
				if(isset($jl_shuom)){
					$jl_danhao= getClassName('kboy_jljilu','jl_danhao',"uid='$uid' and aid = '$aid'");
							if($jl_danhao){
								mysql_query("update kboy_jljilu set jl_danhao='$WIDout_trade_no' where uid='$uid' and aid = '$aid'");
							}else{
								$mysql = new sqlstr("kboy_jljilu");
								$mysql -> set("uid",$uid);
								$mysql -> set("aid",$aid);
								$mysql -> set("jl_shuom",$jl_shuom);
								$mysql -> set("jl_danhao",$rUnique);
								$mysql -> set("jl_jine",$money);
								$mysql -> set("jl_time",date("Y-m-d H:i:s", time()));
								$news_sql = $mysql -> insertSql();
								$news_res = mysql_query($news_sql);
							}
				}else{
					$benren = getClassName("kboy_forum_active","active_cva_money","id=$aid");
					$money = ($order_num-1)*$moneys+$benren;
				}
					
			}else{
				$benren = getClassName("kboy_forum_active","active_cva_money","id=$aid");
				$money = ($order_num-1)*$moneys+$benren;
			}
		}
	}
}
$money_all = ($money*100);//订单金额
 $input->SetBody($subject);
	$input->SetAttach($subject);
	$input->SetOut_trade_no($rUnique);
	$input->SetTotal_fee($money_all);
	$input->SetTime_start(date("YmdHis"));
	$input->SetTime_expire(date("YmdHis", time() + 600));
	$input->SetGoods_tag($subject);
	$input->SetNotify_url("http://www.chinawalking.net.cn/weipay/notify.php");//回调地址
	$input->SetTrade_type("JSAPI");
	$input->SetOpenid($openId);
	$order = WxPayApi::unifiedOrder($input);
/*	echo '<font color="#f00"><b>统一下单支付单信息</b></font><br/>';
	printf_info($order);
*/	$jsApiParameters = $tools->GetJsApiParameters($order);
	
	//获取共享收货地址js函数参数
	$editAddress = $tools->GetEditAddressParameters();

//③、在支持成功回调通知中处理成功之后的事宜，见 notify.php
/**
 * 注意：
 * 1、当你的回调地址不可访问的时候，回调通知会失败，可以通过查询订单来确认支付是否成功
 * 2、jsapi支付时需要填入用户openid，WxPay.JsApiPay.php中有获取openid流程 （文档可以参考微信公众平台“网页授权接口”，
 * 参考http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html）
 */
?>

<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/> 
    <title>中国徒步网-微信支付</title>
    <script type="text/javascript">
	//调用微信JS api 支付
	function jsApiCall()
	{
		WeixinJSBridge.invoke(
			'getBrandWCPayRequest',
			<?php echo $jsApiParameters; ?>,
			function(res){
				WeixinJSBridge.log(res.err_msg);
				if(res.err_msg == 'get_brand_wcpay_request:ok'){
					window.location.href='../sj/personal.php';//支付成功后跳转的地址
					//alert(res.err_msg);
				}else{
					alert(res.err_code+res.err_desc+res.err_msg);
				}
			}
		);
	}

	function callpay()
	{
		if (typeof WeixinJSBridge == "undefined"){
		    if( document.addEventListener ){
		        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
		    }else if (document.attachEvent){
		        document.attachEvent('WeixinJSBridgeReady', jsApiCall); 
		        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
		    }
		}else{
		    jsApiCall();
		}
	}
	//获取共享地址
	function editAddress()
	{
		WeixinJSBridge.invoke(
			'editAddress',
			<?php echo $editAddress; ?>,
			function(res){
				var value1 = res.proviceFirstStageName;
				var value2 = res.addressCitySecondStageName;
				var value3 = res.addressCountiesThirdStageName;
				var value4 = res.addressDetailInfo;
				var tel = res.telNumber;
				
				alert(value1 + value2 + value3 + value4 + ":" + tel);
			}
		);
	}
	
	//window.onload = function(){
//		if (typeof WeixinJSBridge == "undefined"){
//		    if( document.addEventListener ){
//		        document.addEventListener('WeixinJSBridgeReady', editAddress, false);
//		    }else if (document.attachEvent){
//		        document.attachEvent('WeixinJSBridgeReady', editAddress); 
//		        document.attachEvent('onWeixinJSBridgeReady', editAddress);
//		    }
//		}else{
//			//editAddress();
//		}
//	};
	</script>
</head>
<body  >
    <br/>
	  <div class="ok_order">
		<ul>
			  <dl style="margin:0 auto; text-align:center;">
	         <h1 style=" font-size:20px; color:#000; margin:10px 0px">恭喜您，订单创建成功！</h1>
             <h1 style=" font-size:18px; margin-bottom:20px; width:300px;">
			 <span style="color:#000">订单号:<p><strong style=" color:#c90800"><?php echo $rUnique;?></p></strong></span></h1>
			 &nbsp;<span style="color:#000">金额为：<strong style=" color:#c90800"><?php echo ($money_all/100);?>元</strong></span>
             </dl>
           </ul> 
        </div>
	
	<div align="center">
		<button style="width:210px; height:50px; border-radius: 15px;background-color:#FE6714; border:0px #FE6714 solid; cursor: pointer;  color:white;  font-size:20px;" type="button" onClick="callpay()" >立即支付</button>
	</div>
</body>
</html>